package com.cn.qtms.exception;

import com.cn.qtms.common.result.ErrorCode;
import com.cn.qtms.common.result.Result;
import com.cn.qtms.common.result.ResultUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author User
 */
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

    /**
     * 前端web token的key值
     */
    private static final String WEB_TOKEN = "INTERESTSACME_TOKEN";

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException, ServletException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(400);
        System.out.println(authException.getLocalizedMessage());
        Result<Object> error = ResultUtil.error(ErrorCode.INVALIDTOKEN);
        new ObjectMapper().writeValue(response.getOutputStream(), error);
    }
}
